package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.DestinationTheme;
import cn.wolfcode.luowowo.article.domain.Region;
import cn.wolfcode.luowowo.article.mapper.DestinationMapper;
import cn.wolfcode.luowowo.article.mapper.RegionMapper;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.common.query.QueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import sun.security.krb5.internal.crypto.Des;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationMapper destinationMapper;
    @Autowired
    private RegionMapper regionMapper;

    public PageInfo selectForList(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Destination> list = destinationMapper.selectForList(qo);
        return new PageInfo(list);
    }

    public List<Destination> getToasts(Long parentId) {
        ArrayList<Destination> list = new ArrayList<>();

        //存入一开始的目的地
        Destination dest = destinationMapper.selectByPrimaryKey(parentId);
        list.add(dest);

        //不断地获取父级目的地
        if (dest != null) {
            while (dest.getParent() != null) {
                dest = destinationMapper.selectByPrimaryKey(dest.getParent().getId());
                list.add(dest);
            }
        }

        //排序
        Collections.reverse(list);
        return list;
    }

    public AjaxResult chageHotState(Long id, Integer hot) {
        AjaxResult result = new AjaxResult();
        try {
            destinationMapper.chageHotState(id,hot);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    public List<Destination> list() {
        return destinationMapper.selectAll();
    }

    public List<Destination> listDestByRegionId(Long rid) {
        Region region = regionMapper.selectByPrimaryKey(rid);
        List<Destination> list = destinationMapper.selectByids(region.getRefIds());
        return list;
    }

    public List<Destination> listHotDestByRegionId(Long regionId) {
        //区域id>0, 查询区域下的国家
        if(regionId > 0){
            return destinationMapper.selectByRegionId(regionId);
        }else { //区域id小于0,查询中国的省份
            return destinationMapper.selectProvinces();
        }
    }

    public AjaxResult updateInfo(Long id, String info) {
        AjaxResult result = new AjaxResult();
        try {
            destinationMapper.updateInfo(id,info);
        }catch (Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }
        return result;
    }

    public Destination getCountry(Long destId) {
        List<Destination> toasts = getToasts(destId);
        return toasts.get(0);
    }

    public Destination getProvince(Long destId) {
        List<Destination> toasts = getToasts(destId);
        return toasts.size() > 1 ? toasts.get(1) : null;
    }

    public List<Destination> listTop7ByParentId(Long destId) {
        Destination dest = destinationMapper.selectByPrimaryKey(destId);
        return destinationMapper.listTop7ByParentId(dest.getParent().getId());
    }
}
